Skip to main content

Internal data structure

The underlying caching layer for all models is the redux store. All data ends up there so you can inspect the data for all models using the Redux DevTools.

When you open up the Redux DevTools you will roughly find the following structure in the state tab.

type InternalDataStructure = {
entities: {
[ModelName: string]: {
lists: {
[id: string]: ModelList;
};
items: {
[id: string]: ModelItem;
};
};
};
};

type Status =
| 'idle'
| 'loading'
| 'loaded'
| 'fetching'
| 'refreshing'
| 'updating'
| 'deleting'
| 'error';

type ModelList = {
items: string[];
status: Status;
errors: null | any;
consumers: string[];
pagination: PaginationObject;
args?: any;
etag: string;
};

type ModelItem = {
data: EntryData;
status: Status;
errors: null | any;
consumers: string[];
related: {
[fieldName: string]: RelatedModel;
};
source: 'list' | 'item';
args?: any;
etag: string;
};

type RelatedModel = {
type: string;
items: string[];
};

Normalised data

As opposed to a simply caching mechanism, model generator does not simply save the API responses in a store. It can identify individual items in a response object and place those items in their model items list. Each model entry will be placed in a normalised key value map. The key is the id of the entry and the value is the data plus some other metadata (ModelItem type).

Even when fetching a list of items, model generator will take each item and put it into its model item list and then simply reference their ids in a list. This process is called normalisation.

Etag

Each normalised item will have an etag field in their metadata, which is simply an indicator for changes to the entry data. In contrast to the id of an item, which will be unchanged over the lifetime of the item, the etag can change over time.

Once the item is loaded it has an etag assigned to it. Hooks that use that item will not change (re-render) unless the etag of the item changes.

As a user of the model generator, you generally don't need to worry about the etag. Only when you are writing your own reducers for models will you have to know to change the etag in order for updates to be rendered.